/**
 * 
 * @param {路由懒加载} path 
 */
const page = path => () => import(`@/views/${path}`)
import Vue from 'vue'
import Router from 'vue-router'
import ErrorPage from '@/views/error.vue'
import Login from '@/views/login'
import RouterView from '@/components/router-view'

Vue.use(Router)

const page = path => () => import(`@/views/${path}`)

const router = new Router({
  // mode: 'history',
  base: process.env.BASE_URL,
  routes: [
    {
      path: '/',
      name: 'App',
      meta: { title: '个人' },
      redirect: '/account'
    },
    {
      path: '/index',
      name: 'index',
      meta: { title: '首页', auth: true },
      component: page('index')
    },
    {
      path: '/user',
      name: 'user',
      meta: { title: '用户', auth: true },
      component: page('user')
    },
    {
      path: '/class',
      name: 'class',
      meta: { title: '分析', auth: true },
      component: page('class')
    },
    {
      path: '/login',
      name: 'login',
      component: Login,
      meta: { title: '登录' }
    },
    {
      path: '*',
      name: 'error',
      component: ErrorPage,
      meta: { title: '页面找不到了' }
    }
  ]
})

router.beforeEach((to, from, next) => {
  const { title } = to.meta || {}
  // const { title, auth } = to.meta || {}
  // const { userId } = Vue.store.getters.userInfo

  // if (!userId && auth) {
  //   next('/login')
  //   return
  // }

  document.title = title
  next()
})

export default router
